package cn.nfwx.tv.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.widget.ImageView;

import java.lang.ref.SoftReference;

/**
 * Created by zhouzechao on 2018/3/5.
 */
public class AnimationsContainer {
    /**
     * @param iv       播放动画的控件
     * @param frameRes 播放的图片数组
     * @param duration 每帧动画的播放间隔(毫秒)
     */
    public static FramesSequenceAnimation createAnim(ImageView iv, int[] frameRes, int duration) {
        return new FramesSequenceAnimation(iv, frameRes, duration);
    }

    public interface OnAnimationStoppedListener {
        void onAnimationStart();

        void onAnimationStopped();
    }

    public final static class FramesSequenceAnimation {
        private int[] mFrames;
        private int mIndex;
        private boolean mShouldRun;
        private boolean mIsRunning;
        private SoftReference<ImageView> mSoftReferenceImageView;
        private Handler mHandler;
        private int mDelayMillis;
        private OnAnimationStoppedListener mOnAnimationStoppedListener;

        private Bitmap mBitmap = null;
        private BitmapFactory.Options mBitmapOptions;

        public FramesSequenceAnimation(ImageView imageView, int[] frames, int duration) {
            mHandler = new Handler();
            mFrames = frames.clone();
            mIndex = -1;
            mSoftReferenceImageView = new SoftReference<>(imageView);
            mShouldRun = false;
            mIsRunning = false;
            mDelayMillis = duration;

            imageView.setImageResource(mFrames[0]);
            if (imageView.getDrawable() != null && mBitmapOptions == null) {
                Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                int width = bmp.getWidth();
                int height = bmp.getHeight();
                Bitmap.Config config = bmp.getConfig();
                mBitmap = Bitmap.createBitmap(width, height, config);
                mBitmapOptions = new BitmapFactory.Options();
                mBitmapOptions.inBitmap = mBitmap;
                mBitmapOptions.inMutable = true;
                mBitmapOptions.inSampleSize = 1;
            }
        }

        private int getNext() {
            mIndex++;
            if (mIndex >= mFrames.length)
                mIndex = 0;
            return mFrames[mIndex];
        }

        public void start() {
            mShouldRun = true;
            if (mIsRunning) {
                return;
            }

            if (mOnAnimationStoppedListener != null) {
                mOnAnimationStoppedListener.onAnimationStart();
            }

            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    ImageView imageView = mSoftReferenceImageView.get();
                    if (!mShouldRun || imageView == null) {
                        mIsRunning = false;
                        if (mOnAnimationStoppedListener != null) {
                            mOnAnimationStoppedListener.onAnimationStopped();
                        }
                        return;
                    }

                    mIsRunning = true;
                    mHandler.postDelayed(this, mDelayMillis);

                    if (imageView.isShown()) {
                        int imageRes = getNext();
                        if (mBitmap != null) {
                            Bitmap bitmap = null;
                            try {
                                bitmap = BitmapFactory.decodeResource(imageView.getResources(), imageRes, mBitmapOptions);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (bitmap != null) {
                                imageView.setImageBitmap(bitmap);
                            } else {
                                imageView.setImageResource(imageRes);
                            }
                        } else {
                            imageView.setImageResource(imageRes);
                        }
                    }

                }
            };

            mHandler.post(runnable);
        }

        public void stop() {
            mShouldRun = false;
        }

        public void setmOnAnimationStoppedListener(OnAnimationStoppedListener mOnAnimationStoppedListener) {
            this.mOnAnimationStoppedListener = mOnAnimationStoppedListener;
        }
    }
}
